"""
Test recommendations module 
"""

from django.test import TestCase
from RSM.my_friends.models import RSMUser, User
from RSM.my_friends.views import getUserNames
from RSM.my_movies.views import __updateUserMoviesList
from RSM.util import getRSMUser
from RSM.algorithm.search.makeCache import makeTestCache
getUserNames = getUserNames
updateUserMoviesList = __updateUserMoviesList
getRSMUser = getRSMUser
makeTestCache = makeTestCache


class RecommendationsTest(TestCase):
    added = False
    testCount = 1
    
    def setUp(self):
        print "\nRecommendations Test" + str(RecommendationsTest.testCount) + "\n"
        if (RecommendationsTest.added):
            return
        #adding some users to the system
        user1 = User.objects.create_user("Tomer", "t@t.com", "1234")
        user1.save()
        RSMUser(user=user1).save()
        user2 = User.objects.create_user("Eran", "t@t.com", "1234")
        user2.save()
        RSMUser(user=user2).save()
        user2 = User.objects.create_user("Alex", "t@t.com", "1234")
        user2.save()
        RSMUser(user=user2).save()
        #add some movies to the system
        makeTestCache()
        #create test setup
        #Setting movie data
        selectedList = ["Mov1","Mov2"]
        username = "Tomer"
        movieType = "Seen"
        updateUserMoviesList(username,selectedList,movieType)
        selectedList = ["Mov4"]
        username = "Tomer"
        movieType = "Blacklist"
        updateUserMoviesList(username,selectedList,movieType)
        selectedList = ["Mov2","Mov5"]
        username = "Eran"
        movieType = "Seen"
        updateUserMoviesList(username,selectedList,movieType)
        selectedList = ["Mov5","Mov1"]
        username = "Alex"
        movieType = "Seen"
        updateUserMoviesList(username,selectedList,movieType)
        #setting friendships
        self.client.login(password=u"1234",username="Tomer")
        self.client.post("/friends/activeAdd/", {"friendsToAdd":("Eran","Alex")})
        #setting groups
        self.client.post("/groups/activeAdd/", {"groupname":"testGroup1"})
        self.client.post("/groups/editfriends/", {"friends":("Eran","Alex"),"action":"addFriend","groupname":"testGroup1"})

                
    def tearDown(self):
        print "\nRecommendations Test" + str(RecommendationsTest.testCount) + "\n"
        RecommendationsTest.testCount += 1
        
        
    def test_1_basicRecommend(self):
        print "basic recommend: ask for recommendation to self based only on seen & black list"
        self.client.login(password=u"1234",username="Tomer")
        response = self.client.post("/recommendation/recommend/")
        self.assertContains(response, "Mov3")
        self.assertContains(response, "Mov5")
        self.assertNotContains(response, "Mov4")
        self.assertNotContains(response, "Mov1")
        self.assertNotContains(response, "Mov2")
        print "Only unseen and non blacklisted movies were recommended"
        
    def test_2_recommendSeen(self):
        print "recommend with seen movies: ask for recommendation to self based only on black list"
        self.client.login(password=u"1234",username="Tomer")
        response = self.client.post("/recommendation/recommend/",{"recommendSeen":"true"})
        self.assertContains(response, "Mov3")
        self.assertContains(response, "Mov5")
        self.assertNotContains(response, "Mov4")
        self.assertContains(response, "Mov1")
        self.assertContains(response, "Mov2")
        print "Only non blacklisted movies were recommended"
        
    def test_3_recommendWithFriends(self):
        print "recommend with friends: ask for recommendation to self based on self & friends seen & black list"
        self.client.login(password=u"1234",username="Tomer")
        response = self.client.post("/recommendation/recommend/",{"friendList":("Alex","Eran")})
        self.assertContains(response, "Mov3")
        self.assertNotContains(response, "Mov5")
        self.assertNotContains(response, "Mov4")
        self.assertNotContains(response, "Mov1")
        self.assertNotContains(response, "Mov2")
        print "Only unseen and non blacklisted movies were recommended considering friends movies as well"
        
    def test_4_recommendWithGroups(self):
        print "recommend with groups: ask for recommendation to self based on self & group seen & black list"
        self.client.login(password=u"1234",username="Tomer")
        response = self.client.post("/recommendation/recommend/",{"groupList":"testGroup1"})
        self.assertContains(response, "Mov3")
        self.assertNotContains(response, "Mov5")
        self.assertNotContains(response, "Mov4")
        self.assertNotContains(response, "Mov1")
        self.assertNotContains(response, "Mov2")
        print "Only unseen and non blacklisted movies were recommended considering group movies as well"
        
    def test_5_recommendWithGroupsVsFriends(self):
        print "comparing group and friends results"
        self.client.login(password=u"1234",username="Tomer")
        response1 = self.client.post("/recommendation/recommend/",{"groupList":"testGroup1"})
        response2 = self.client.post("/recommendation/recommend/",{"friendList":("Alex","Eran")})
        self.assertEqual(str(response1),str(response2))
        print "the results are equal"
        
    
    def test_6_basicRecommend(self):
        print "selected recommend: ask for recommendation to self based only on seen & black list & chosen movies"
        self.client.login(password=u"1234",username="Tomer")
        response = self.client.post("/recommendation/recommend/", {"selectedList":("Mov3","Mov2")})
        self.assertContains(response, "Mov3")
        self.assertNotContains(response, "Mov5")
        self.assertNotContains(response, "Mov4")
        self.assertNotContains(response, "Mov1")
        self.assertNotContains(response, "Mov2")
        print "Only unseen and non blacklisted movies from the list were recommended"
        
        